/**
 * 彩票的接口服务
 */
class LotteryService {
    constructor(constant, $http, $q,$cookies, responseService, ajaxError, Http) {
        Object.assign(this, {constant, $http, $q,$cookies, responseService, ajaxError, Http});
        this.lotteryType = null;
        this.lotteryName = null;
        //彩票期次信息
        this.lotteryIssueInfo = null;
        // {
        //     "lot_id": "43",
        //     "lot_name": "幸运赛车",
        //     "issueid": "17091653",
        //     "time_startsale": "2017-09-26 21:14:52",
        //     "time_endsale": "2017-09-26 21:24:52",
        //     "time_openaward": "2017-09-26 21:26:52"
        // }
    }

    setLotteryIssueInfo(lotteryIssueInfo){
        let self = this;
        self.lotteryIssueInfo = lotteryIssueInfo;
    }

    getLotteryIssueInfo(){
        let self = this;
        self.getCurrentIssue(self.lotteryType)
            .then(_currentIssueSucc)
            .catch(_currentIssueError);

        function _currentIssueSucc(data) {
            if(data.message && data.message.code ==200){
                self.lotteryIssueInfo =data;
                self.setLotteryIssueInfo(data);
                return self.lotteryIssueInfo;
            }else{
                return self.$q.reject(data.message);
            }
        }

        function _currentIssueError(data) {
            //弹出警告窗口
            console.log("LotteryService.getCurrentIssue() :"+JSON.stringify(data));
        }
    }

    /**
     * 获取当前期次信息
     * @param lotteryType 彩种类型
     */
    getCurrentIssue(lotteryType) {
        let self = this;
        let param = {"lot_type": lotteryType};
        return self.Http.post("api/get_curr_issue", param,{ignoreLoadingBar: true});
    }

    /**
     *获取开奖公告
     */
    getWinningInfo() {
        let self = this;
        let param = {"lot_type": self.lotteryType};
        return self.Http.post("api/get_winning_num", param,{ignoreLoadingBar: true});
    }

    /**
     * 根据选号计算投注金额
     * @param ticketCodeList ticketCodeList
     */
    getBetCalcAmount(ticketCodeList) {
        let self = this;
        let param = {"lot_type": self.lotteryType};
        angular.merge(param, ticketCodeList);
        return self.Http.post("api/get_bet_calc_amount", param,{ignoreLoadingBar: true});

    }

    /**
     * 投注
     * @param ticketCodeListParam
     */
    bet(ticketCodeListParam){
        let self = this;
        ticketCodeListParam.issueid = self.lotteryIssueInfo.issueid;
        ticketCodeListParam.lot_type = self.lotteryType;
        return self.Http.post("api/lottery_bet", ticketCodeListParam);
    }

    /**
     * 获取彩种列表
     */
    getLottyTypes(){
        let self = this;
        return self.Http.post("api/get_lottery_type", {});
    }

    /**
     * 获取彩种下的开奖号码
     * @param lotteryType 彩种
     */
    getWinningNum(lotteryType){
        let self = this;
        let param = {};
        param.lot_type= lotteryType;
        return self.Http.post("api/get_winning_num", param);
    }

    getWonUserList(lotteryType){
        let self = this;
        let param = {};
        param.lot_type= lotteryType;
        return self.Http.post("api/get_winning_list_byuser", param);
    }

    get_award_description(param,config){
        let self = this;
        let config1 = config || {};
        return self.Http.post("api/get_award_description", param,config1);
    }

    get_lottery_winning_num_list(param,config){
        let self = this;
        let config1 = config || {};
        return self.Http.post("api/get_lottery_winning_num_list", param,config1);
    }
}

LotteryService.$inject =
    ["Constant", "$http", "$q","$cookies", "ResponseService", 'AjaxErrorHandler', "Http",];

export default LotteryService;